<?php

namespace app\app\controller;

use app\app\model\ProjectReportRemindModel;
use app\app\model\RandModel;
use app\app\model\TodoModel;
use app\BaseController;
use app\app\model\ProjectReportModel;


class ProjectReport extends BaseController
{
    public function index()
    {
        $data = $this->request->param();
        $page_size = input('pageSize', '20');


        $where = [];
        if (!empty($data['start_time']) && !empty($data['end_time'])) {
            $where[] = ['a.oper_time', 'between', [$data['start_time'], $data['end_time']]];
        }

        //项目名查询
        if (isset($data['project_name'])) {
            $where[] = ['b.project_name|c.project_name', 'like', '%' . $data['project_name'] . '%'];
        }
        $list = ProjectReportModel::alias('a')
            ->field('a.id as report_id,b.link_project_id,b.project_name as project_name,b.id,c.project_name as project_main_name,c.id as main_id')
            ->leftjoin('t_project b', 'a.project_id = b.id')
            ->leftjoin('t_project c', 'b.link_project_id = c.id')
            ->where($where)->paginate($page_size)->toArray();
        $info = $list['data'];

        $tmp_arr = array();
        $temp = array();
        foreach ($info as $k => $v) {
            if (!in_array($v['main_id'], $tmp_arr)) {
                $tmp_arr[$k] = $v['main_id'];
                $temp[$k]['id'] = $v['main_id'];
                $temp[$k]['project_name'] = $v['project_main_name'];
            }
        }
        foreach ($temp as $key => $value) {
            foreach ($info as $k => $v) {
                if ($value['id'] == $v['link_project_id']) {
                    $temp[$key]['children'][] = $v;
                }
            }
            $list['data'] = $temp;
        }

        $this->success('success', $list);
    }

    //单体列表
    public function list()
    {
        $id = input('id', '');
        $list = ProjectReportModel::where('project_id', $id)->select();
        $this->success('success', $list);
    }

    //单体信息
    public function info()
    {
        $id = input('id', '');
        $list = ProjectReportModel::where('id', $id)->select();
        $this->success('success', $list);
    }

    /**
     * 周报一键通知
     */
    public function remind()
    {
        $type = input('type', '1');
        $project_id = input('project_id', '1');
        $timestamp = time();
        $start_time = date('Y-m-d 00:00:00', strtotime("this week Monday", $timestamp));
        $end_time = date("Y-m-d 23:59:59", strtotime("$start_time +6 day"));

        if ($type == 1) {  //意见通知
            $remind_list = ProjectReportRemindModel::where('create_time', 'between', [$start_time, $end_time])->where('state', '未发送')->select();
            if (count($remind_list)) {
                $todo_task_list = array();
                $data_task_list = array();
                foreach ($remind_list as $key => $value) {
                    $data['id'] = RandModel::randId();
                    $data['project_id'] = $value['project_id'];
                    $data['user_name'] = $value['user_name'];
                    $data['yj_content'] = '周报上传提醒';
                    $data['yj_type'] = '系统消息';
                    $data['yj_date'] = date('Y-m-d 00:00:00', time());
                    $data['project_name'] = $value['project_name'];
                    $data['create_user'] = '系统';
                    $data['receive_user'] = $value['user_name'];
                    $data['end_date'] = $end_time;
                    $data['state'] = 1;
                    $data['is_read'] = 1;

                    $item['id'] = RandModel::randId();
                    $item['link_id'] = $data['id'];
                    $item['receive_user'] = $value['user_name'];
                    $item['status'] = 1;
                    $item['end_date'] = $end_time;

                    array_push($todo_task_list, $item);
                    array_push($data_task_list, $data);

                }
                (new \app\app\model\TodoTaskModel)->saveAll($todo_task_list, false);
                (new \app\app\model\TodoModel)->saveAll($data_task_list, false);
                $remind_data['state'] = '已发送';
                ProjectReportRemindModel::where('create_time', 'between', [$start_time, $end_time])
                    ->where('state', '未发送')->update($remind_data);

            }
            $todo_task_list = array();
        } else {  //按项目通知
            $remind_list = ProjectReportRemindModel::where('create_time', 'between', [$start_time, $end_time])
                ->where('project_id', $project_id)
                ->where('state', '未发送')->select();
            if (count($remind_list)) {
                $todo_task_list = array();
                $data_task_list = array();
                foreach ($remind_list as $key => $value) {
                    $data['id'] = RandModel::randId();
                    $data['project_id'] = $value['project_id'];
                    $data['user_name'] = $value['user_name'];
                    $data['yj_content'] = '周报上传提醒';
                    $data['yj_type'] = '系统消息';
                    $data['yj_date'] = date('Y-m-d 00:00:00', time());
                    $data['project_name'] = $value['project_name'];
                    $data['create_user'] = '系统';
                    $data['receive_user'] = $value['user_name'];
                    $data['end_date'] = $end_time;
                    $data['state'] = 1;
                    $data['is_read'] = 1;


                    $item['id'] = RandModel::randId();
                    $item['link_id'] = $data['id'];
                    $item['receive_user'] = $value['user_name'];
                    $item['status'] = 1;
                    $item['end_date'] = $end_time;
                    array_push($todo_task_list, $item);
                    array_push($data_task_list, $data);

                }
                (new \app\app\model\TodoTaskModel)->saveAll($todo_task_list, false);
                (new \app\app\model\TodoModel)->saveAll($data_task_list, false);


                $remind_data['state'] = '已发送';
                ProjectReportRemindModel::where('create_time', 'between', [$start_time, $end_time])
                    ->where('project_id', $project_id)
                    ->where('state', '未发送')->update($remind_data);
            }
        }

        $this->success('success');
    }


    public function add()
    {
        $data = $this->request->param();
        $data['oper_time'] = date('Y-m-d H:i:s', time());
        ProjectReportModel::create($data);
        $this->success('success');
    }

    public function detail()
    {
        $id = input('project_id');

        $timestamp = time();

//        $start_time = date('Y-m-d 00:00:00', strtotime("this week Monday", $timestamp));
//        $end_time = date("Y-m-d 23:59:59", strtotime("$start_time +6 day"));

        $list = ProjectReportModel::where('project_id', $id)->select();
        $this->success('success', $list);
    }

}
